package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.PageResult;
import com.woniu.mapper.ArticleMapper;
import com.woniu.mapper.MarkMapper;
import com.woniu.pojo.Article;
import com.woniu.pojo.CarouselChart;
import com.woniu.pojo.Mark;
import com.woniu.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.QuerySearchArticleVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author doujiao
 * @since 2022-10-11
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private MarkMapper markMapper;

    /**
     * 条件搜索+分页查询article
     * @param currentPageStr
     * @param pageSizeStr
     * @param articleVo
     * @return
     */
    public PageResult queryByPage(String currentPageStr, String pageSizeStr, QuerySearchArticleVo articleVo) {
        //转int
        int currentPage = Integer.parseInt(currentPageStr);
        int pageSize = Integer.parseInt(pageSizeStr);
        PageResult pageResult = new PageResult();
        //构造分页
        IPage<Article> page=new Page(currentPage,pageSize);
        //如果搜索条件不为空
        if (articleVo!=null){
            QueryWrapper<Article> wrapper=new QueryWrapper<>();
            if (articleVo.getCreateTime()!=null){
                wrapper.gt("create_time",articleVo.getCreateTime());
            }
            if (articleVo.getTitle()!=null){
                wrapper.like("title",articleVo.getTitle());
            }
            //查询
            IPage<Article> articleIPage = articleMapper.selectPage(page, wrapper);
            pageResult.setTotal(articleIPage.getTotal());
            pageResult.setData(articleIPage.getRecords());
            return pageResult;
        }
        //如果没有查询条件
        IPage<Article> articleIPage = articleMapper.selectPage(page, null);
        pageResult.setTotal(articleIPage.getTotal());
        pageResult.setData(articleIPage.getRecords());
        return pageResult;
    }
    /**
     * 查询轮播图排序的最大值
     * @return
     */
    @Override
    public Integer queryMaxSortByCourseId() {
        return articleMapper.selectMaxSortByCourseId();
    }

    /**
     * 修改轮播图发布状态
     * @param articleId
     * @return
     */
    @Override
    public boolean saveStatus(Long articleId) {
        UpdateWrapper<Article> updateWrapper = new UpdateWrapper<>();
        Article article = articleMapper.selectById(articleId);
        if(!StringUtils.isEmpty(article.getStatus()) && article.getStatus().equals("0")){
            updateWrapper.set("status", "1");
            updateWrapper.eq("id", articleId);
            return update(updateWrapper);
        }
        if(!StringUtils.isEmpty(article.getStatus()) && article.getStatus().equals("1")){
            updateWrapper.set("status", "0");
            updateWrapper.eq("id", articleId);
            return update(updateWrapper);
        }
        return false;
    }

    /**
     * 修改文章收藏数量
     * @param articleId
     * @return
     */
    @Override
    public boolean updateCollectNumByMark(Long articleId) {
        QueryWrapper<Mark> wrapper = new QueryWrapper<>();

        wrapper.eq("article_id",articleId);
        Long count = markMapper.selectCount(wrapper);
        String s = count.toString();
        Article article = articleMapper.selectById(articleId);
        article.setCollectNum(s);
        return false;
    }
}
